from sqlalchemy import Column, Integer, String, DateTime, DECIMAL, Boolean, Text, Float

from app.models.base import BaseModel


class Order(BaseModel):
    """订单表"""
    __tablename__ = "order"
    __table_args__ = {"comment": "订单主表"}

    order_id = Column(Integer, primary_key=True, autoincrement=True, comment='订单业务主键')
    order_no = Column(String(32), unique=True, comment='订单号')
    total_price = Column(DECIMAL(10, 2), comment='商品总金额 (不含优惠折扣)')
    order_price = Column(DECIMAL(10, 2), comment='订单金额 (含优惠折扣)')
    coupon_id = Column(Integer, comment='优惠券ID')
    coupon_money = Column(DECIMAL(10, 2), comment='优惠券抵扣金额')
    points_money = Column(DECIMAL(10, 2), comment='积分抵扣金额')
    points_num = Column(Integer, comment='积分抵扣数量')
    pay_price = Column(DECIMAL(10, 2), comment='实际付款金额 (包含运费)')
    buyer_remark = Column(Text, comment='买家留言')
    pay_type = Column(Integer, comment='支付方式（余额/微信/支付宝）')
    pay_status = Column(Integer, comment='付款状态(10未付款 20已付款)')
    pay_time = Column(DateTime, comment='付款时间')
    delivery_type = Column(Integer, comment='配送方式 (10快递配送 20门店自提)')
    express_price = Column(DECIMAL(10, 2), comment='运费金额')
    express_company = Column(String(50), default='', comment='物流公司')
    express_no = Column(String(50), default='', comment='物流单号')
    delivery_status = Column(Integer, comment='发货状态 (10未发货 20已发货 30部分发货)')
    delivery_time = Column(DateTime, comment='发货时间')
    receipt_status = Column(Integer, comment='收货状态 (10未收货 20已收货)')
    receipt_time = Column(DateTime, comment='收货时间')
    order_status = Column(Integer, comment='订单状态 (10进行中 20取消 21待取消 30已完成)')
    order_source_id = Column(Integer, comment='来源记录ID')
    platform = Column(String(10), comment='下单客户端 (APP、H5、小程序等)')
    user_id = Column(Integer, index=True, comment='用户ID')
    is_delete = Column(Boolean, comment='是否删除0-否 1-是')

    class OrderDetail(BaseModel):
        """订单明细表"""
        __tablename__ = "order_detail"
        __table_args__ = {"comment": "订单明细"}

        order_detail_id = Column(Integer, primary_key=True, autoincrement=True, comment='明细ID')
        order_id = Column(Integer, comment='订单ID')
        goods_id = Column(Integer, comment='商品ID')
        goods_name = Column(String(500), comment='商品名称')
        image_id = Column(Integer, comment='封面图文件ID')
        deduct_stock_type = Column(Integer, comment='库存计算方式 10=下单减')
        spec_type = Column(Integer, comment='规格类型 10=单规格')
        goods_sku_id = Column(String(50), comment='SKU-ID')
        goods_props = Column(String(500), comment='所选规格属性 JSON')
        goods_no = Column(String(50), comment='商品编码')
        goods_price = Column(DECIMAL(10, 2), comment='单价')
        line_price = Column(DECIMAL(10, 2), comment='划线价')
        goods_weight = Column(Float, comment='重量(kg)')
        is_user_grade = Column(Boolean, comment='是否启用会员价')
        grade_ratio = Column(Integer, comment='会员折扣比例 0-100')
        grade_goods_price = Column(DECIMAL(10, 2), comment='会员价')
        grade_total_money = Column(DECIMAL(10, 2), comment='会员总价')
        coupon_money = Column(DECIMAL(10, 2), comment='优惠券分摊')
        points_money = Column(DECIMAL(10, 2), comment='积分分摊')
        points_num = Column(Integer, comment='抵扣积分数量')
        points_bonus = Column(Integer, comment='赠送积分')
        total_num = Column(Integer, comment='购买数量')
        total_price = Column(DECIMAL(10, 2), comment='小计金额')
        total_pay_price = Column(DECIMAL(10, 2), comment='实付小计')
        is_comment = Column(Boolean, comment='是否已评价')
        user_id = Column(Integer, comment='用户ID')
        goods_source_id = Column(Integer, comment='来源业务ID')
        goods_image = Column(String(255), comment='封面图URL')
        refund = Column(String(50), comment='退款单号（为空表示无退款）')
